package com
{	
	import com.control.PushData;
	import com.event.EventData;
	
	import events.CaptchaEvent;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import models.CaptchaModel;
	
	import views.ViewCaptcha;
	import views.ViewData;

	public class BoardHandler extends MovieClip
	{
		private var mcBoard:MovieClip;
		private var btnAdd:MovieClip,btnRemove:MovieClip,btnCaptcha:MovieClip;
		private var mcCaptcha:MovieClip;
		private var txtLabel:TextField,txtCaptcha:TextField;
		private var pushData:PushData;
		private var pushCaptcha:CaptchaModel;		

		public function BoardHandler()
		{
			setup();
			txtLabel.text="Message here";			
			pushData=new PushData();
			pushData.addEventListener(EventData.ON_ADD_DATA,listenerEvent);
			pushData.addEventListener(EventData.ON_REMOVE_DATA,listenerEvent);
			btnAdd.addEventListener(MouseEvent.CLICK,clickHandler);
			btnRemove.addEventListener(MouseEvent.CLICK,clickHandler);
			btnCaptcha.addEventListener(MouseEvent.CLICK,clickHandler);
			// captcha event					
			pushCaptcha=new CaptchaModel(mcCaptcha);			
			pushCaptcha.addEventListener(CaptchaEvent.ON_CHANGE_CAPTCHA,listenerEventCaptcha);
			// call list
			//new ViewData(mcBoard.parent.list_mc);
			//trace(this.parent);
		}
		private function clickHandler(evt:MouseEvent):void
		{
			if(evt.target.label=="addData"){
				pushData.doAdd();				
			} else if(evt.target.label=="getCaptcha"){
				pushCaptcha.getCaptcha();	
			}
			else{
				pushData.doRemove();
			}
		}
		private function setup():void
		{
			mcBoard=this;
			mcCaptcha=mcBoard.captcha_mc;
			btnAdd=mcBoard.add_mc;
			btnRemove=mcBoard.remove_mc;
			btnCaptcha=mcBoard.getCaptcha_mc;
			btnAdd.buttonMode=btnRemove.buttonMode=btnCaptcha.buttonMode=true;
			btnAdd.mouseChildren=btnRemove.mouseChildren=btnCaptcha.mouseChildren=false;
			
			var ftText:TextFormat=new TextFormat();
			ftText.align=TextFormatAlign.CENTER;
			
			var txtBtnAdd:TextField=btnAdd._txt;
			txtBtnAdd.text="Add";
			btnAdd.label="addData";
			
			var txtBtnRemove:TextField=btnRemove._txt;
			txtBtnRemove.text="Remove";
			btnRemove.label="removeData";
			
			txtLabel=mcBoard.label_txt;	
			txtLabel.defaultTextFormat=ftText;
			
			var txtBtnCaptcha:TextField=btnCaptcha._txt;
			txtBtnCaptcha.text="View Captcha";
			btnCaptcha.label="getCaptcha";
			
			txtCaptcha=mcBoard.captcha_txt;
			txtCaptcha.defaultTextFormat=ftText;
		}	
		private function listenerEvent(evt:EventData):void
		{
			txtLabel.text=evt.customMess;				
		}
		private function listenerEventCaptcha(evt:CaptchaEvent):void
		{
			txtCaptcha.text=evt.dataCaptcha;			
		}
	}
}